﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedLib.XmlDataModel;
using System.Xml;
using SharedLib.Helpers;
namespace Tracy.DataModel
{
    public class Entry : XmlDataModelWithAliases
    {

        public Entry(XmlNode node) : base(node) { }
        public Entry() : base() { }
        public Entry(string searchKeywords) : base()
        {
            Name = searchKeywords.Replace(" ", "");
            SearchKeywords = searchKeywords;
            RegExpr = GenerateRegExpr(searchKeywords);
        }
        //TODO: ctor with additional properties

        public static string GenerateRegExpr(string searchKeywords)
        {
            return ".*" + searchKeywords.Replace("+", "\\+").Replace("*", "\\*").Replace(".", "\\.").Replace(" ", ".*") + ".*"; 
        }

        /// <summary>
        /// Name of the Entry
        /// </summary>
        public override string Name
        {
            get { return Attributes["name"]; }
            set { Attributes["name"] = value; }
        }

        /// <summary>
        /// [EntryName] ([UnwatchedCount])
        /// </summary>
        public string NameWithUnwatchedCount
        {
            get
            {
                int unWatchedCount = TracyFacade.Instance.MediaFileProvider.GetUnwatchedCount(this);
                if (unWatchedCount > 0)
                    return String.Format("{0} ({1})", Name, unWatchedCount);
                else
                    return Name;
            }
        }

        /// <summary>
        /// Romaji of the Entry
        /// </summary>
        public string Romaji
        {
            get { return Attributes["romaji"]; }
            set { Attributes["romaji"] = value; }
        }

        /// <summary>
        /// Will use this keyword to generate url
        /// </summary>
        public string SearchKeywords
        {
            get { return Properties["search-keywords"]; }
            set { Properties["search-keywords"] = value; }
        }

        /// <summary>
        /// Regular Expression to match resource
        /// </summary>
        public string RegExpr
        {
            get { return Properties["reg-expr"]; }
            set { Properties["reg-expr"] = value; }
        }

        /// <summary>
        /// Physical path to save downloaded files
        /// </summary>
        public string SavePath //TODO: support removable devices
        {
            get
            {
                string rtn = Properties["save-path"];
                if (rtn == null) return null;
                if (!rtn.EndsWith("\\")) rtn += "\\";
                return rtn;
            }
            set { Properties["save-path"] = value; }
        }

        public bool TracingDisabled
        {
            get { return String.Equals(Attributes["tracing-disabled"], "true",StringComparison.OrdinalIgnoreCase); }
            set
            {
                if (value) Attributes["tracing-disabled"] = "true";
                else Attributes.Remove("tracing-disabled"); //default value is false
            }
        }

        public override string RootNodeName
        {
            get
            {
                return "entry";
            }
        }

        //public string ID
        //{
        //    get { return Attributes["id"]; }
        //    internal set { Attributes["id"] = value; }
        //}

        
    }
}
